<link href="__PUBLIC__/css/style.default.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="__PUBLIC__/js/html5shiv.js"></script>
<script src="__PUBLIC__/js/respond.min.js"></script>
<![endif]-->

<div class="mainpanel">
    <div class="pageheader">
        <div class="media">
            <div class="pageicon pull-left">
                <i class="fa fa-th-list"></i>
            </div>
            <div class="media-body">
                <ul class="breadcrumb">
                    <li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
                    <li><a href="">首页</a></li>
                    <li>账号管理</li>
                </ul>
                <h4>账号管理</h4>
            </div>
        </div><!-- media -->
    </div>

    <div class="contentpanel">

        <div class="panel panel-primary-head">
            <div id="succ_file_s" class="alert alert-success hides">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <strong>账号添加成功</strong>
            </div>

            <div id="faild_file_s" class="alert alert-danger hides">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <strong>账号添加失败,请检查！</strong>
            </div>

            <div class="row" style="padding: 0 15px">
                <!--<button id="USER_MADOL_ADD_BTN" class="btn btn-primary" >添加用户</button>-->


            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <div class="panel-btns" style="display: none;">
                                <a href="" class="panel-minimize tooltips maximize" data-toggle="tooltip" title="" data-original-title="Minimize Panel">
                                    <i class="fa fa-plus"></i>
                                </a>
                            </div><!-- panel-btns -->
                            <h4 class="panel-title">添加账号</h4>
                            <p style="    color: #fff;">点击右上角显示更多</p>
                        </div><!-- panel-heading -->
                        <div class="panel-body" style="display: none;">
                            <form id="user_add_ajaxForm" method="post" action="{:url('admin/Padaccount/add')}" enctype="multipart/form-data">

                                <div class="row">
                                    <div class="row">
                                        <div class="form-group col-md-4">
                                            <label>账号名称</label>
                                            <input type="text" name="title" placeholder="账号名称" class="form-control">
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label>套餐类别</label>

                                            <select name="type" class="form-control">
                                                {volist name="emsconfig['padgradeconfig']" id="vo" key="k"}
                                                <option value="{$vo.xb}">{$vo.val}</option>
                                                {/volist}
                                            </select>
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label>状态</label>

                                            <select name="status" class="form-control">
                                                <option value="1">正常</option>
                                                <option value="2">禁用</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="form-group col-md-4">
                                            <label>账号</label>
                                            <input type="text" name="name" placeholder="账号" class="form-control">
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label>价格</label>
                                            <input type="text" name="price" placeholder="价格" class="form-control">
                                        </div>
                                    </div>


                                </div><!-- row -->
                                <label>账号简介</label>
                                <textarea name="desc" class="form-control" rows="5" placeholder="账号简介"></textarea>
                            </form>

                        </div><!-- panel-body -->

                        <div class="panel-footer" style="display: none;">
                            <button id="USER_ADD_FROM_BTN" class="btn btn-primary">添加</button>
                        </div><!-- panel-footer -->
                    </div><!-- panel -->

                </div>
            </div>

            <table id="exRowTable" class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>账号名称</th>
                    <th>账号</th>
                    <th>类别</th>
                    <th>价格</th>
                    <th>创建时间</th>
                    <th>所在机构</th>
                    <th>教练</th>
                    <th>登录状态</th>
                    <th>状态</th>
                    <th>最后登录时间</th>
                    <th>描述</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div><!-- panel -->

    </div>

</div>
<!--madol 添加用户 hide-->
<div id="user_add_madol" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">&times;</button>
                <h4 class="modal-title">分配账号</h4>
            </div>
            <div class="modal-body">
                <form id="org_edit_ajaxForm" method="post" action="{:url('admin/Padaccount/distribution')}" enctype="multipart/form-data">
                    <input type="hidden" name="type">
                    <input type="hidden" name="id">
                </form>
                    <div class="row">

                        <div class="col-md-12">
                            <div class="row">
                                <label>选择机构</label>
                                <select id="org_code_param_upd" name="org_code"  class="form-control">

                                    <option value="" selected>请选择</option>
                                    {volist name="org_list" id="vo"}
                                    <option value="{$vo.org_code}">{$vo.org_name}</option>
                                    {/volist}
                                </select>
                            </div>
                            <div class="row hides">
                                <label>已选设备</label>
                                <div class="col-md-12" >
                                    <div class="table-responsive">
                                        <table class="table">
                                            <caption></caption>
                                            <thead>
                                            <tr>
                                                <th>设备名称</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody id="dev_list_code">

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <button style="float: right;" id="ORG_EDIT_FROM_BTN" class="btn btn-primary">提交</button>

                            </div>
                        </div>
                        <div class="col-md-8 hides">
                            <label>选择设备</label>
                            <table id="devRowTable" class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>设备名称</th>
                                    <th>设备编码</th>
                                    <th>设备MAC</th>
                                    <th>设备类型</th>
                                    <th>设备颜色</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>

                <div class="row" style="padding-right: 1rem;padding-top: 1rem" >
                    <!--<button style="float: right;" id="ORG_EDIT_FROM_BTN" class="btn btn-primary">提交</button>-->
                </div>
            </div>
        </div>
    </div>
</div>

<div id="user_distribution_madol" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">&times;</button>
                <h4 class="modal-title">分配账号</h4>
                <input id="tcid" type="hidden">
            </div>
            <div class="modal-body">

                <div class="row">
                    <div class="col-md-12">
                        <label>选择教练</label>
                        <table id="userRowTable" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>用户名</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>

                <div class="row" style="padding-right: 1rem;padding-top: 1rem" >
                    <!--<button style="float: right;" id="ORG_EDIT_FROM_BTN" class="btn btn-primary">提交</button>-->
                </div>
            </div>
        </div>
    </div>
</div>

<div id="upd_user_distribution_madol" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">&times;</button>
                <h4 class="modal-title">修改账号信息</h4>
            </div>
            <div class="modal-body">

                <div class="row">
                    <div class="col-md-12">
                        <form id="user_upd_ajaxForm" method="post" action="{:url('admin/Padaccount/add')}" enctype="multipart/form-data">

                            <div class="row">
                                <div class="row">
                                    <div class="form-group col-md-4">
                                        <label>账号名称</label>
                                        <input type="text" name="title" placeholder="账号名称" class="form-control">
                                        <input type="hidden" name="acc_id">
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>套餐类别</label>

                                        <select name="type" class="form-control" disabled>
                                            {volist name="emsconfig['padgradeconfig']" id="vo" key="k"}
                                            <option value="{$vo.xb}">{$vo.val}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>状态</label>
                                        <select name="status" class="form-control" disabled>
                                            <option value="1">正常</option>
                                            <option value="2">禁用</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="form-group col-md-4">
                                        <label>账号</label>
                                        <input type="text" name="name" placeholder="账号" class="form-control" disabled>
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>价格</label>
                                        <input type="text" name="price" placeholder="价格" class="form-control" disabled>
                                    </div>
                                </div>


                            </div><!-- row -->
                            <label>账号简介</label>
                            <textarea name="desc" class="form-control" rows="5" placeholder="账号简介"></textarea>
                        </form>
                    </div>
                </div>

                <div class="row" style="padding-right: 1rem;padding-top: 1rem" >
                    <button style="float: right;"  onclick="updacc()" class="btn btn-primary">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>


<style>
    .hides{
        display: none;
    }

</style>
<script>
    jQuery('#select-search-hide').select2({
        minimumResultsForSearch: -1
    });

    var exRowTable;
    var devRowTable;
    var devarr = [];
    var devobj = {};
    $(function(){

        $("#USER_MADOL_ADD_BTN").click(function () {
            $("#user_add_madol").modal("show")
        })


        // 添加账号
        $("#USER_ADD_FROM_BTN").click(function () {

            var org_name = $("#user_add_ajaxForm").find('[name = "name"]').val()

            // 账号
            if(org_name == null || org_name == undefined || org_name == ''){
                $("#user_add_ajaxForm").find('[name = "org_name"]').focus()
                // $("#user_add_ajaxForm").find('[name = "org_name"]').css('border','1px solid red')
                return;
            }

            $("#user_add_ajaxForm").ajaxSubmit(function (data) {
                console.log(data)
                if(data == 0){
                    $(".hides").hide()
                    $("#succ_file_s").html("添加失败")
                    $("#faild_file_s").show()
                }else{
                    $(".hides").hide()
                    $("#succ_file_s").html("添加成功")
                    $("#succ_file_s").show()
                }
                exRowTable.ajax.reload();
            });


        })
        // 修改账号
        $("#ORG_EDIT_FROM_BTN").click(function () {

            // 核对设备数量
        /*    if($("#org_edit_ajaxForm [name=type]").val()!=devarr.length){
               alert('设备数量保不正确')
                return
            }
*/
            // 核对机构
            var orgcode = $('#org_code_param_upd').val()
            if(!orgcode){
                alert('请选择机构')
                return
            }

            var id =$('#org_edit_ajaxForm [name=id]').val()
            $.post("{:url('admin/Padaccount/distributionorg')}",{org_code:orgcode,id:id,devid:devarr},function(data,status){
                console.log(data)
                if(data !== 0){

                    devRowTable.ajax.reload();
                    exRowTable.ajax.reload();
                    alert('添加成功')
                }else{
                    devRowTable.ajax.reload();
                    exRowTable.ajax.reload();
                    alert('添加失败')


                }
                // alert("Data: " + data + "nStatus: " + status);
            });

        })

        $("[data-toggle='tooltip']").tooltip();

        exRowTable = $('#exRowTable').DataTable({
            responsive: true,
            "fnDrawCallback": function(oSettings) {
                jQuery('#exRowTable_paginate ul').addClass('pagination-active-success');
            },
            "ajax": "{:url('admin/padaccount/query')}",
            "columns": [
                { "data": "id" },
                { "data": "title" },
                { "data": "name" },
                { "data": "type" , "render": function ( data, type, full, meta ) {
                        switch(data){
                            case '1':
                                return '1对1';
                                break;
                            case '6':
                                return '1对6';
                            default:
                                return '默认';
                                break;
                        }
                    }
                    },
                { "data": "price" },
                { "data": "create_time" },
                { "data": "org_name" },
                { "data": "user_name" },
                { "data": "login_mark", "render": function ( data, type, full, meta ) {
                        switch(data){
                            case '1':
                                return '未登录';
                                break;
                            case '0':
                                return '已登录';
                                break;
                            case '3':
                                return '禁用';
                                break;
                            default:
                                return '默认';
                                break;
                        }
                    } },
                { "data": "status", "render": function ( data, type, full, meta ) {
                        switch(data){
                            case '1':
                                return '入库';
                                break;
                            case '2':
                                return '已分配';
                                break;
                            case '3':
                                return '禁用';
                                break;
                            default:
                                return '默认';
                                break;
                        }
                    }
                    },
                { "data": "last_online_time" },
                { "data": "desc", "render": function ( data, type, full, meta ) {

                    if(data.length>10){
                        return  '<span title="'+data+'">'+data.slice(0,10)+'...</span>'

                        // return data.slice(0,10)+"..."
                    }else {
                        return data

                    }


                    } },
                { "data": "id", "render": function ( data, type, full, meta ) {
                       switch (full.status){
                           case '1':
                                       {switch name="$Think.session.user.user_role" }
                                           {case value="3" break="0或1"}
                                           return '<button onclick="distributionjl('+data+','+full.type+')" class="btn btn-primary btn-xs" >分配</button>';
                                       {/case}
                                           {default /}
                                           return '<button onclick="padaccupdview('+data+','+full.type+')" class="btn btn-primary btn-xs" >修改</button> <span>  </span><button onclick="org_edit_view('+data+','+full.type+')" class="btn btn-primary btn-xs" >分配</button> <span>  </span>' +
                                               // '<button onclick="org_edit_view('+data+')" class="btn btn-primary btn-xs" >修改</button> <span> ' +
                                               ' </span><button onclick="org_upd_acc('+full.id+',3)" class="btn btn-danger btn-xs">停用</button>'+
                                               ' </span><button onclick="org_del('+data+')" class="btn btn-danger btn-xs">删除</button>';

                                       {/switch}

                               break;
                                       case '3':
                                           return '<button onclick="org_upd_acc('+data+',1)" class="btn btn-primary btn-xs" >启用</button> <span>  </span>'
                                           break;

                           default:
                               return '';
                               break;

                       }
                    }
                },

            ],
            "order": [[0, 'desc']]
        });

        devRowTable = $('#devRowTable').DataTable({
            responsive: true,
            "fnDrawCallback": function(oSettings) {
                jQuery('#exRowTable_paginate ul').addClass('pagination-active-success');
            },
            "ajax": "{:url('admin/device/devicequery')}",
            "columns": [
                { "data": "dev_sn" },
                { "data": "dev_name" },
                { "data": "dev_code" },
                { "data": "dev_mac" },
                { "data": "dev_type" },
                { "data": "dev_color","render": function ( data, type, full, meta ) {
                        return '<button type="button" class="btn btn-primary btn-xs" style="background-color: '+data+';color: '+data+'">修改</button>';
                    } },
                { "data": "dev_sn","render": function ( data, type, full, meta ) {
                        return '<button onclick="dev_pad_save('+data+',\''+full.dev_name+'\')" class="btn btn-primary btn-xs">选中</button>';
                    }},
            ],
            "order": [[0, 'desc']]
        });
        userRowTable = $('#userRowTable').DataTable({
            responsive: true,
            "fnDrawCallback": function(oSettings) {
                jQuery('#exRowTable_paginate ul').addClass('pagination-active-success');
            },
            "ajax": "{:url('admin/user/userquery')}",
            "columns": [
                { "data": "user_acc_sn" },
                { "data": "acc_name" },
                { "data": "create_date" },

                { "data": "user_acc_sn","render": function ( data, type, full, meta ) {
                        return '<button onclick="fpjl('+data+',\''+full.acc_name+'\')" class="btn btn-primary btn-xs">分配</button>';
                    }},
            ],
            "order": [[0, 'desc']]
        });





    })

    function org_del(id) {
        if(confirm('确认删除！')){

            $.post("{:url('admin/padaccount/del')}",{ID:id},function (data) {

                if(data == 0){
                    $(".hides").hide()
                    $("#faild_file_s").html("修改失败")
                    $("#faild_file_s").show()
                }else if(data == 'error'){
                    $(".hides").hide()
                    $("#faild_file_s").html("该账号为状态不为入库，不能删除")
                    $("#faild_file_s").show()
                }else{
                    $(".hides").hide()
                    $("#succ_file_s").html("删除成功")
                    $("#succ_file_s").show()
                }
                exRowTable.ajax.reload();
            })
        }
    }

    // 显示修改界面
    function org_edit_view(id,type) {

        $("#org_edit_ajaxForm [name=type]").val(type)
        $("#org_edit_ajaxForm [name=id]").val(id)
        $("#user_add_madol").modal('show')
    }

    // 添加到左侧框操作
    function dev_pad_save(sn,name) {

        // 账号设备限制数量
        if($("#org_edit_ajaxForm [name=type]").val()>devarr.length){
            if(devarr.indexOf(sn)<0){
                devarr.push(sn)
                devobj[sn] =name
            }
        }else{

            alert('设备数量为账号限制数量')
        }
        // console.log(devarr.indexOf())
        // console.log(devarr)
        // console.log(devobj)
        putbutton()
    }

    // 添加button
    function putbutton() {
        $("#dev_list_code").html('')
        $.each(devarr,function (i,v) {
            // $("#dev_list_code").append('<button class="btn btn-success" data="'+v+'">'+devobj[v]+'</button>')
            $("#dev_list_code").append('<tr> <td>'+'<button class="btn btn-success  btn-xs">'+devobj[v]+'</button>'+'</td> <td><button onclick="delbutton('+v+')" class="btn btn-danger btn-xs" data="'+v+'">删除</button></td> </tr>')
            // console.log(v)
            // console.log(devobj[v])
        })
    }

    // 删除button
    function delbutton(sn) {
        delete devobj[sn]
        var index = $.inArray(sn,devarr);
        devarr.splice(index,1)
        putbutton()
    }

    // 显示分配界面
    function distributionjl(id) {

        $("#tcid").val(id)
        $("#user_distribution_madol").modal('show')

    }
    
    function fpjl(jlsn,acc_name) {
      /*  console.log($("#tcid").val())
        console.log(jlsn)
        console.log(acc_name)*/
        $.post("{:url('admin/padaccount/addjl')}",{ID:$("#tcid").val(),user_name:acc_name},function (data) {

            if(data == 0){
                $(".hides").hide()
                $("#faild_file_s").html("修改失败")
                $("#faild_file_s").show()
            }else if(data == 'error'){
                $(".hides").hide()
                $("#faild_file_s").html("该账号为状态不为入库，不能操作")
                $("#faild_file_s").show()
            }else{
                $(".hides").hide()
                $("#succ_file_s").html("操作成功")
                $("#succ_file_s").show()
            }
            exRowTable.ajax.reload();
        })
    }
    // 修改设备状态
    function org_upd_acc(id,status) {
        $.post("{:url('admin/padaccount/updstatus')}",{ID:id,status:status},function (data) {

            if(data == 0){
                $(".hides").hide()
                $("#faild_file_s").html("操作失败")
                $("#faild_file_s").show()
            }else if(data == 'error'){
                $(".hides").hide()
                $("#faild_file_s").html("操作失败")
                $("#faild_file_s").show()
            }else{
                $(".hides").hide()
                $("#succ_file_s").html("操作成功")
                $("#succ_file_s").show()
            }
            exRowTable.ajax.reload();
        })
    }

    // 修改账号view
    function padaccupdview(id) {

        /*var inputupdaccount = $("#user_upd_ajaxForm input")
        console.log(inputupdaccount)*/
        $.post("{:url('admin/padaccount/sel')}",{ID:id},function (data) {
            $("#user_upd_ajaxForm [name = title]").val(data[0]['title'])
            $("#user_upd_ajaxForm [name = type]").val(data[0]['type'])
            $("#user_upd_ajaxForm [name = status]").val(data[0]['status'])
            $("#user_upd_ajaxForm [name = name]").val(data[0]['name'])
            $("#user_upd_ajaxForm [name = price]").val(data[0]['price'])
            $("#user_upd_ajaxForm [name = desc]").val(data[0]['desc'])
            $("#user_upd_ajaxForm [name = acc_id]").val(data[0]['id'])

        })

        $("#upd_user_distribution_madol").modal('show')
    }

    // 修改账号
    function updacc() {
        var ID =  $("#user_upd_ajaxForm [name = acc_id]").val()
        var name =  $("#user_upd_ajaxForm [name = name]").val()
        var desc =  $("#user_upd_ajaxForm [name = desc]").val()
        $.post("{:url('admin/padaccount/updpadacc')}",{id:ID,name:name,desc:desc},function (data) {
            if(data == 0){
                $(".hides").hide()
                $("#faild_file_s").html("修改失败")
                $("#faild_file_s").show()
            }else if(data == 'error'){
                $(".hides").hide()
                $("#faild_file_s").html("修改失败，数据库内部错误")
                $("#faild_file_s").show()
            }else{
                $(".hides").hide()
                $("#succ_file_s").html("修改成功")
                $("#succ_file_s").show()
            }
            exRowTable.ajax.reload();

        })

    }
</script>



